﻿-- Function: tepuy.nextval(character varying, character varying)

-- DROP FUNCTION tepuy.nextval(character varying, character varying);

CREATE OR REPLACE FUNCTION tepuy.nextval(character varying, character varying)
  RETURNS integer AS
$BODY$
-- Copyright © 2010 Riztan Gutierrez <riztan at gmail dot com>
-- Esto es parte de: proyecto tepuy.
-- Este programa es Software Libre conforme a la GPL Version 3 o posterior.
-- http://www.gnu.org/licenses/
DECLARE
   res integer;
   query varchar;
   field       varchar;
   schematable varchar;
BEGIN

   field       := $1;
   schematable := $2;

   query := 'select max(' || field || ')+1 as newvalue from ' ||schematable;

   execute query into res;

   return res;
END;
$BODY$
  LANGUAGE 'plpgsql' VOLATILE
  COST 100;
ALTER FUNCTION tepuy.nextval(character varying, character varying) OWNER TO postgres;
COMMENT ON FUNCTION tepuy.nextval(character varying, character varying) IS 'retorna proximo valor para el campo tipo integer en la tabla dada';
